﻿using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using QunYing.Model;

namespace QunYing.Web.market
{
    public partial class scoredetail : System.Web.UI.Page
    {
        Model.GoodsInfo model = null;
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater rp = ((Repeater)Master.FindControl("NavRepeater"));
                rp.DataSource = NavData();
                rp.DataBind();
            }
        }
        /// <summary>
        /// 快捷导航数据
        /// </summary>
        private DataTable NavData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("url");
            DataTable dtNode = null;
            if (model != null)
            {
                dtNode = new BLL.Category().GetParentCategory(model.类别ID.ToString());

                if (dtNode.Rows.Count > 0)
                {
                    dt.Rows.Add("积分兑换", "scoregoods.aspx");
                    for (int i = 0; i < dtNode.Rows.Count; i++)
                    {
                        dt.Rows.Add(dtNode.Rows[i]["类别名称"].ToString(), "scoregoods.aspx?category=" + dtNode.Rows[i]["类别ID"]);
                    }
                    dt.Rows.Add(model.商品名称, "");
                }
                else
                {
                    dt.Rows.Add("积分兑换", "");
                }
            }
            else
            {
                dt.Rows.Add("积分兑换", "");
            }

            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["id"] != null)
            {
                model = new BLL.GoodsInfo().GetGoodsModel(Request["id"]);
            }
            if (!IsPostBack)
            {
                SetValue();
            }

        }
        /// <summary>
        /// 绑定信息
        /// </summary>
        private void SetValue()
        {
            if (model != null)
            {
                DataTable dt = new BLL.GoodsImg().GetGoodsImg(model.商品ID.ToString());
                StringBuilder sb = new StringBuilder();
                sb.Append(@"<div class='jqzoom' id='spec-n1' >");
                sb.Append(@"<img height='300' width='300' src='" + model.DefaultImg + "' jqimg='" + model.DefaultImg + "' >");
                sb.Append(@"</div>
	                        <div id='spec-n5'>
		                        <div class='control' id='spec-left'>
			                        <img src='images/left.gif' />
		                        </div>
		                        <div id='spec-list'>
			                        <ul class='list-h'>");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append(@"<li><img src='" + dt.Rows[i]["图片路径"] + "'> </li>");
                }
                sb.Append(@"</ul>
		                    </div>
		                    <div class='control' id='spec-right'>
			                    <img src='images/right.gif' />
		                    </div>
                        </div>");

                lblImg.Text = sb.ToString();
                lblName.Text = model.商品名称;
                lblBrand.Text = model.商品品牌;
                lblDesc.Text = model.商品描述;
                lblScore.Text = model.兑换积分.ToString();
                lblUnit.Text = model.单位;
            }
        }

        protected void btnBuy_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (model != null)
            {
                ShopCart.AddGoods(model, Enums.Score);
                Response.Redirect("~/orderone.aspx");
            }
            else
            {
                MessageBox.Show(this, "出现异常,请刷新重试!");
            }
        }

        protected void btnCollect_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (model != null)
            {
                BLL.Collect collect = new BLL.Collect();
                Model.Collect collectModel = new Collect();
                collectModel.用户ID = ((ArrayList)Session["userInfo"])[0].ToString();
                collectModel.商品ID = model.商品ID;
                collectModel.收藏类别 = Enums.Score;
                collectModel.收藏时间 = DateTime.Now;
                if (collect.ExistsGoods(model.商品ID, Enums.Score))
                {
                    MessageBox.Show(this, "收藏成功!");
                    return;
                }
                collect.Add(collectModel);
                MessageBox.Show(this, "收藏成功!");
            }
            else
            {
                MessageBox.Show(this, "收藏失败,请刷新重试!");
            }
        }
    }
}
